<?php

namespace app\shop\controller;

use app\api\service\WechatMiniService;
use app\common\model\Account;
use app\common\model\User;
use app\common\service\Status;
use buildView\Actions;
use buildView\Button;
use think\Controller;
use buildView\Grid;
use app\shop\model\ShopWithdrawal;
use think\Db;
use think\Exception;

/**
 * 提现记录
 * Class Withdrawal
 * @package app\shop\controller
 */
class Withdrawal extends Controller
{
    /**
     * 提现记录
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $grid = new Grid(new ShopWithdrawal);
        $grid->setTitle('提现记录');
        $grid->user()->userInfo();
        $grid->money('实际到账金额');
        $grid->withdrawal_money('提现金额');
        $grid->service_money('手续费');
        $grid->status('状态')->using(Status::WITHDRAWSTATUS())->badge();

        $grid->create_at('申请提现时间');
        $grid->hideDeletesButton();
        $grid->hideAddButton();
        $grid->hideExportButton();
        $grid->actions(function (Actions $action, $data) {
            $action->hideDetail();
            $action->hideDel();
            $action->hideEdit();
            if ($data['status'] == 1) {
                $button = new Button('马上打款', 'info', 'sm', '');
                $button->save($data['id'], ['status'=> 2], url('pass'),'确认打款？');
                $action->append($button);
                $button = new Button('拒绝打款', 'danger', 'sm', '');
                $button->save($data['id'], ['status'=> 3], url('refused'),'确认拒绝打款？');
                $action->append($button);
            } else {
                $action->append($data['action_time']);
            }
        });
        $grid->filter(function ($filter) {
            $filter->like('user.nickname', '会员昵称');
            $filter->eq('status', '状态')->select(Status::WITHDRAWSTATUS());
            $filter->dateBetween('creata_at', '申请提现时间');
        });
        return $grid->view();
    }

    /**
     * 拒绝打款
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/10/11 10:01
     */
    public function refused()
    {
        Db::startTrans();
        try {
            $withdrawal = ShopWithdrawal::find($this->request->id);
            $withdrawal->status = 3;
            $withdrawal->action_time = date('Y-m-d H:i:s');
            $withdrawal->save();
            User::where('id', $withdrawal['uid'])->setInc('money', $withdrawal['withdrawal_money']);
            Account::create([
                'uid' => $withdrawal['uid'],
                'title' => "申请失败",
                'desc' => "申请失败 ￥{$withdrawal['withdrawal_money']}",
                'money' => $withdrawal['withdrawal_money'],
                'type' => 2,
            ]);
            Db::commit();
            $this->success('操作成功', '');
        } catch (Exception $e) {
            Db::rollback();
            $this->error('操作失败', '');
        }
    }

    /**
     * 同意打款
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/10/11 9:59
     */
    public function pass()
    {
        Db::startTrans();
        try {
            $withdrawal = ShopWithdrawal::find($this->request->id);
            $withdrawal->status = 2;
            $withdrawal->action_time = date('Y-m-d H:i:s');
            $withdrawal->save();
            $openid = User::where('id',$withdrawal['uid'])->value('openid');
            $res = WechatMiniService::transfersPay($openid,$withdrawal['money'],'提现到账');
            if($res){
                Db::commit();
                $this->success(' 打款成功','');
            }else{
                Db::rollback();
                $this->error('打款失败', '');
            }
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('打款失败:'.$e->getMessage(), '');
        }
    }
}
